<?php

class Admin_StaticPageController extends My_Controller_ActionAdmin {

    public function indexAction() {
        $daoEvent = new Application_Model_DAO_StaticPage();
        $list = $daoEvent->getEntries()->query()->fetchAll();
        $this->view->list = $list;
    }

    public function saveAction() {
        $code = $this->_getParam('id');
        $isEdit = false;
        if (!empty($code)) {
            $daoStaticPage = new Application_Model_DAO_StaticPage();
            $record = $daoStaticPage->getStaticPageByColumn($code, 'code');
            $form = new Admin_Form_StaticPage_Edit();
            if (!$this->_request->isPost()) {
                $record['id'] = $code;
                $form->populate($record);
            }
            $isEdit = true;
        } else {
            $form = new Admin_Form_StaticPage_Add();
        }
        if (!$this->_request->isPost() || !$form->isValid($_POST)) {
            $this->view->form = $form;
            $this->renderForm();
            return;
        }
        $data = $form->getValues();
        unset($data['id']);
        $daoStaticPage = new Application_Model_DAO_StaticPage();
        if ($isEdit) {
            $record = $daoStaticPage->update($record['static_page_id'], $data);
            $this->addSuccess($this->translate('XXX_ADMIN_STATIC_PAGE_EDIT_SUCCESS'));
        } else {
            $record = $daoStaticPage->addNew($data);
            $this->addSuccess($this->translate('XXX_ADMIN_STATIC_PAGE_ADD_SUCCESS'));
        }        
        $url = $this->url(array('controller' => 'static-page', 'action' => 'index'));
        $this->_redirect($url);
    }  
}

